<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6 0006
 * Time: 15:47
 */

namespace English\Controller;
use Admin\Model\UserModel;
use app\index\controller\SmsApi;
use Think\Controller;

class PublicController extends Controller
{

    protected $user;
    public function __construct()
    {
        parent::__construct();
        $this->user = new UserModel();
        if(!getSysStatus()){
            header("Content-type:text/html;charset=utf-8");

            exit('系统维护中....');
        }
    }


    public function login()
    {
        if(session('User_yctr')){
            $this->redirect('User/index');
        }
        if(IS_POST){
            $param = I('post.');


            $param['password'] = md5($param['password']);

            if(!($info = M('user')->where($param)->find())){
                ajax_return(1,'用户名或者密码错误');
            }
//            ajax_return(1,'维护期，晚上6点以后可以登录使用');
            if($info['status'] == 2){
                // ajax_return(1,'您的账户未激活');
                ajax_return(1,'账户异常');
            }

//            if($info['is_f'] == 2){
//                ajax_return(1,'您的账户已经被冻结');
//            }

//            if($info['status'] != 2){
//                ajax_return(1,'请等待管理员开通');
//            }
            session('User_yctr',$info);
            $this->makeAdminLogin();
            ajax_return(0,'登录成功',U('Index/index'));
        }else{
            $this->assign('username',I('get.username'));
            $this->display();
        }
    }





    //发送短信
//    public function sendCode()
//    {
//        if(IS_POST){
//            include 'smsapi.fun.php';
//            $uid = 'txhtsms0023';
//            $pwd = '833b1e88baac27c3e7641d477aac89a6';
//            $data['mobile'] = I('phone');
//            if(empty($data['mobile'])){
//                $this->ajaxreturn(array(code=>'1',message=>'请输入手机号'));
//            }
//            //           $msg_list = M('msg_list')->where('mobile = "'.$data['mobile'].'"')->order('id desc')->find();
//            //            if(time()-$msg_list['time']<120){
//            //                $this->ajaxreturn(array(code=>'1',message=>'请120秒后重试'));
//            //             ajax_return(1,'请120秒后重试');
//            //         }
//            /*$user = M('user')->where('username = "'.$data['mobile'].'"')->find();
//            if(empty($user)){
//                ajax_return(1,'该手机号未注册');
//            }*/
//            $data['code'] = randNumber(4);
//            $data['time'] = time();
//            M('msg_list')->add($data);
//            $content = '【GHA】尊敬的用户，您的验证码为：'.$data['code'].'，10分钟内有效，若非本人操作请忽略';
//            $content1=urlencode(mb_convert_encoding($content ,"gb2312","UTF-8"));
//            $res = sendSMS($uid,$pwd,$data['mobile'],$content1);
//            $this->ajaxreturn(array(code=>'1',message=>'发送成功'));
////            $this->ajaxreturn(array(code=>'1',message=>$res));
//        }
//    }
    public function sendCode()
    {
        if(IS_POST){
            include 'smsapi.fun.php';
            $uid = C('SMS.UID');
            $pwd = C('SMS.PWD');
            $data['mobile'] = I('phone');
            if(empty($data['mobile'])){
                $this->ajaxreturn(array('code'=>'1','message'=>'请输入手机号'));
            }
            //           $msg_list = M('msg_list')->where('mobile = "'.$data['mobile'].'"')->order('id desc')->find();
            //            if(time()-$msg_list['time']<120){
            //                $this->ajaxreturn(array(code=>'1',message=>'请120秒后重试'));
            //             ajax_return(1,'请120秒后重试');
            //         }
            /*$user = M('user')->where('username = "'.$data['mobile'].'"')->find();
            if(empty($user)){
                ajax_return(1,'该手机号未注册');
            }*/
            $data['code'] = randNumber(4);
            $data['time'] = time();
            M('msg_list')->add($data);
           $phone_ex= I('phone_ex')?I('phone_ex'):86;
           if($phone_ex!='86'){
              // $content = '【huidbank】Your verification code is'.$data['code'].', and the validity period is 10 minutes. Please use it within the validity period.';
               $content = '【HBK】Your verification code is'.$data['code'].', and the validity period is 10 minutes. Please use it within the validity period.';
           }else{
               $content = '【HBK】您的手机验证码是'.$data['code'].'，有效期为10分钟，请在有效期内使用。';
           }
            $content1=urlencode(mb_convert_encoding($content ,"gb2312","UTF-8"));
            $res = sendSMS($uid,$pwd,$data['mobile'],$content1);
            $this->ajaxreturn(array('code'=>'1','message'=>'发送成功'));
//            $this->ajaxreturn(array(code=>'1',message=>$res));
        }
    }

    /**
     * 发送短信验证码
     * @return array
     */
    public function SendCode2(){
        //引用发送短信类
        include 'Smsapi.php';
//        require 'Smsapi.php';
        //接口账号
        $uid = 'h675';
        //登录密码
        $pwd = 'mx@451078';

        /**
         * 实例化接口
         *
         * @param string $uid 接口账号
         * @param string $pwd 接口密码
         */
        $api= new SmsApi($uid,$pwd);
//        $api = new SmsApi($uid,$pwd);

        //验证码
        $code = $api->randNumber();

        //发送的手机 多个号码用,英文逗号隔开
        $mobile_prefix =I('phone_ex');  //国家区号
        $phone =   I('phone');   //手机号
        if(empty($phone)){
            $this->ajaxreturn(array('code'=>'1','message'=>'请输入手机号'));
        }

        if ($mobile_prefix == 86){
            $mobile = $phone;
            //发送方式 send国内短信
            $ac = "send";
            //短信内容
//            $content = '【Huione HGS】'.$code.'是您的 HUIONE HGS 验证码，请在10分钟内使用。';
            $content = '【HBK】您的手机验证码是'.$code.'，有效期为10分钟，请在有效期内使用。';
        }else{
            $mobile = $mobile_prefix.$phone;
            //发送方式 send国内短信
            $ac = "sendint";
            //短信内容
            $content = '【HBK】Your verification code is'.$code.', and the validity period is 10 minutes. Please use it within the validity period.';

        }
        //发送全文模板短信
        $result = $api->sendAll($mobile,$content,$ac);
        if($result['stat']=='100'){
            $data['code'] = $code;
            $data['time'] = time();
            $data['mobile'] = I('phone');
            M('msg_list')->add($data);
//            return ['status'=>1,'message'=>"发送成功"];
            $this->ajaxreturn(array('code'=>'1','message'=>'发送成功'));
        }else{
            $this->ajaxreturn(array('code'=>'0','message'=>'发送失败'));
//            return ['status'=>0,'message'=>'Failed to send:'.$result['stat'].'('.$result['message'].')'];
        }
    }
    /**
     * 发送邮箱
     * User: ming
     * Date: 2020/2/19 15:26
     */
    public function send_email()
    {
        $data = I('post.');
        //验证发送时间
//        $codeData = M('msg_list')->where(['tel'=>$data['mobile']])->order('id desc')->find();
        $codeData = M('msg_list')->where(['mobile' => $data['email']])->order('id desc')->find();
//        if(time() - $codeData['addtime'] < 60)
        if (time() - $codeData['time'] < 60) {
//            $this->ajaxReturn(array('code' => '-1', 'message' => '未满一分钟，请稍后再试'));
            $this->ajaxReturn(array('code' => '-1', 'message' => '请在一分钟后再发送'));
        }
//        $this->ajaxReturn(array('code' => $data['email'], 'message' => '发送失败'));
//        if(SendMail($_POST['mail'],$_POST['title'],$_POST['content'])) {
        if (sendMail($data['email'], '验证码')) {
            $this->ajaxReturn(array('code' => '1', 'message' => '发送成功'));

        } else {
            $this->ajaxReturn(array('code' => '-1', 'message' => '发送失败'));

        }
    }

    public function recover(){
        $this->display();
    }

    public function create_wallet(){
        $id=I('id');
        $memory=M('user')->where(array('id'=>$id))->find();
        $word=$memory['memory_word'];
        $wallet_name=$memory['wallet_name'];
        $this->assign('wallet',$wallet_name);
        $this->assign('id',$id);
        $this->assign('word',explode(' ',$word));
        $this->display();
    }


    public function backup_wallet(){
        $id=I('id');
        $this->assign('id',$id);
        $str=M('user')->where(array('id'=>$id))->find();
        $str=$str['memory_word'];
        $word=explode(' ',$str);
        shuffle($word);
        $this->assign('word',$word);
        $this->display();
    }

    //匹配助记词与数据库是否匹配
    public function check_backup(){
        $data = I('post.');
        $word=M('user')->where(array('id'=>$data['id']))->find();
        if($data['word'] === $word['memory_word']){
            ajax_return('1','助记词顺序正确',U('login'));
        }else{
            ajax_return('0','助记词有误');
        }
    }


    //忘记密码 匹配数据库助记词
    public function check_memory(){
        $memory=I('post.memory');
        $memory_arr = M('user')->where('id<>1')->getField('memory_word',true);
        if(in_array($memory,$memory_arr)){
            $id=M('user')->where(array('memory_word'=>$memory))->getField('id',true);
            ajax_return('1',$id);
        }else{
            ajax_return('0','助记词有误，请重新输入');
        }
    }


    public function find_password()
    {
        if(IS_POST){
            $param = I('post.');
            if(!$info = $this->user->where(array('email'=>$param['email'],'username'=> $param['username']))->find()){
                ajax_return('0','该邮箱或账号未注册');
            }


            $msg_list = M('msg_list')->where('mobile = "'.$param['email'].'"')->order('id desc')->find();

            if(!$msg_list){
                ajax_return('0','请获取验证码');
            }

             if(time() - $msg_list['time'] > 120){
                 ajax_return(1,'验证码已失效');
             }

            if($param['code'] != $msg_list['code']){
                ajax_return('0','验证码错误');
            }

            if(empty($param['password'])){
                ajax_return('0','登录密码不能为空');
            }

            if($param['password']!==$param['password_re']){
                ajax_return('0','两次登录密码不一致');
            }
            if(empty($param['password2'])){
                ajax_return('0','支付密码不能为空');
            }

            if($param['password2']!==$param['password2_re']){
                ajax_return('0','两次支付密码不一致');
            }

//            $save['id'] = $info['id'];
//            $save['password'] = $param['password'];
//            $save['password2'] = $param['password2'];

            $data['password'] = md5($param['password']);
            $data['password2'] = md5($param['password2']);
            if($this->user->where(array('email'=>$param['email'],'username'=> $param['username']))->save($data) === false){
                ajax_return(0,'密码找回失败');
            }
            ajax_return(1,'提交成功',U('login'));



        }else{
            $this->id=I('get.id');
            $this->display();
        }
    }

    private function test($phone)
    {
 
        include 'smsapi.fun.php';
        $uid = 'tianrui';
        $pwd = 'b167d0f2f913b8843a491ce9d02bfaf5';
        $data['code'] = randNumber(4);
        $data['time'] = time();
        $content = '【cocoin】本次找回密码验证码为'.$data['code'].'，请及时操作。';
        $content1=urlencode(mb_convert_encoding($content ,"gb2312","UTF-8"));
        $res = sendSMS($uid,$pwd,$phone,$content1);
        $this->ajaxReturn('发送成功,请查看手机');



        require_once 'Ucpaas.class.php';
        $options['accountsid']='bd8f02677ed3820aea2d5e0ddb942a0a';
        $options['token']='33901adfe0ee77c83b9ae8d90399912c';
        $appId = 'd7ba246e408648889edaeddd56367a93';
        $to = $phone;
        $templateId = 253918;

//初始化 $options必填

        $ucpass = new \Ucpaas($options);

//开发者账号信息查询默认为json或xml
        $code = randNumber(4);
        $map['code'] = $code;
        $map['phone'] = $phone;
        $map['create_time'] = time();

        M('phone_code')->add($map);

//        echo $ucpass->getDevinfo('xml');
        if($ucpass->templateSMS($appId,$to,$templateId,$code))
        {
            dump(11);die;
        }
        

    }



    public function getOut()
    {
        session('User_yctr',null);
        // session_destroy();
        $this->redirect('Index/index');
    }



    //微信H5支付回调
    public function weixin_h5_notify(){
        die;
        include_once  'wechatH5Pay.php';
        $notify = new \wechatAppPay();
        $xmlObj=simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $out_trade_no = $xmlObj->out_trade_no;//订单号
        $total_fee = $xmlObj->total_fee;//钱
        $return_code = $xmlObj->return_code;
        // $rejg = $notify->orderQuery($out_trade_no);
        file_put_contents("out_trade_no.txt", $return_code);
        $order = M('recharge')->where('id = "'.$out_trade_no.'"')->find();

        if($return_code=='SUCCESS' and $order['status']==1){

            $o = M('recharge');

            $save['id'] = $order['id'];
            $save['status'] = 2;
            $o->save($save);
            //  file_put_contents('log.txt',"【sql】:\n".M()->getLastSql()."\n");

            $this->user->where(array('id'=>$order['user_id']))->setInc('z'.$order['account'],$order['amount']);
            file_put_contents('log.txt',"【sql】:\n".M()->getLastSql()."\n");

            $map['user_id'] = $order['user_id'];
            $map['amount'] = $order['amount'];
            $map['type'] = $order['account'];
            $map['account'] = $order['account'];
            $map['create_time'] = time();
            $map['tips'] ='账户充值';
            $map['flag'] = '+';

            M('money_detail')->add($map);

        }
    }



    // 推广奖与推荐代理奖

    private function rewardByUser($user_id)
    {
        $userinfo = $this->user->find($user_id);
        if($userinfo){

        }
        return true;
    }

    public function notifydfc()
    {
        Vendor('WxPayv3.JSAPI');  
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $out_trade_no = $xmlObj->out_trade_no;//订单号
        $total_fee = $xmlObj->total_fee;//支付价格
        $openid = $xmlObj->openid;//
        $queryorder = new \MicroPay();
        $rejg = $queryorder->query($out_trade_no);

        if ($rejg) {
            //$data = M('reward_config')->find(1);
            //   $temp = explode('_',$out_trade_no);
            //  $out_trade_no = $temp[1];
            $map['order_id'] = $out_trade_no;
            $order = M('order')->where($map)->find();

            if($order['status'] == 1){

//                if($order['status']>1){
//                    exit();
//                }

                $save1['id'] = $order['id'];
                $save1['status'] = 2;
                $save1['time_end']  = strtotime($xmlObj->time_end);

                M('order')->save($save1);
                $db = M();
                if($order['order_type'] == 1){
                    $level = 1;
                    if($order['goods_id']){
                        $ttt = M('goods')->find($order['goods_id']);



                        $level  = $ttt['price'] == 88 ? 2:3;


                    }


                    $u = $this->user->getUserinfoById($order['user_id']);


                    if($level>$u['level']){
                        $save['level'] = $level;
                        $save['id'] = $order['user_id'];
                        $this->user->save($save);

                    }

                    if(!M('tj')->where(['admin_id'=>$order['user_id']])->find()){
                        $userinfo = $this->user->find($order['user_id']);
                        $higher_info = $this->user->find($userinfo['higher_id']);

                        if($higher_info){
                            $id = $order['user_id'];
                            $higher_id = $higher_info['id'];
                            $db->execute("call net_add_tj($id,$higher_id)");
                        }

                    }

                    if($level == 2){

                        for($i = 0 ; $i<$order['num'];$i++){
                            $this->tuijian_reward2($order['user_id']);

                        }

                    }

                    if($level == 3){
                        $this->daili_reward($order['user_id']);
                    }else{
                        for($i = 0 ; $i<$order['num'];$i++) {
                            $this->jiandian($order['user_id']);
                        }
                    }
                    //$this->baodan($order['user_id'],$order['amount']);
                    if($level == 2){
                        //  $this->yeji_reward($order['user_id'],$order['amount']);
                        //   $this->yeji_reward2($order['user_id'],$order['amount']);
                        $this->yeji_reward4($order['user_id'],$order['amount']);
                    }
                }else{

                }
                $a = $order['user_id'];
                $e = $order['amount'];
                $db->execute("call kt001($a,$e)");
                $this->add_min_sale($order);
            }
        }
    }



    public function yyy()
    {
        $user  = M('user')->where(['level',['gt',1]])->select();
        foreach ($user as $k => $v){
            $map['admin_id'] = $k['id'];
        }
    }


    public function notify_url()
    {

        //使用通用通知接口
        vendor('WxPayPubHelper.WxPayPubHelper');
        $notify = new \Notify_pub();

        //存储微信的回调
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $notify->saveData($xml);

        //验证签名，并回应微信。
        //对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
        //微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
        //尽可能提高通知的成功率，但微信不保证通知最终能成功。
        if($notify->checkSign() == FALSE){
            $notify->setReturnParameter("return_code","FAIL");//返回状态码
            $notify->setReturnParameter("return_msg","签名失败");//返回信息
        }else{
            $notify->setReturnParameter("return_code","SUCCESS");//设置返回码
        }
        $returnXml = $notify->returnXml();
        echo $returnXml;

        //==商户根据实际情况设置相应的处理流程，此处仅作举例=======

        //以log文件形式记录回调信息



        // file_put_contents('log.txt',"【接收到的notify通知】:\n".$notify->checkSign()."\n");

        if($notify->checkSign() == TRUE)
        {
            if ($notify->data["return_code"] == "FAIL") {
                //此处应该更新一下订单状态，商户自行增删操作
                file_put_contents('log.txt',"【通信出错】:\n".$xml."\n");
            }
            elseif($notify->data["result_code"] == "FAIL"){
                //此处应该更新一下订单状态，商户自行增删操作
                file_put_contents('log.txt',"【业务出错】:\n".$xml."\n");
            }
            else{
                //此处应该更新一下订单状态，商户自行增删操作
                $out_trade_no = $notify->data["out_trade_no"];

                $order = M('order')->where('order_id = "'.$out_trade_no.'"')->find();
                //  file_put_contents('log.txt',"【sql】:\n".M()->getLastSql()."\n");

                if($order['status'] == 2){
                    $this->success('订单已经支付',U('index/index'));
                    exit();
                }

                $o = M('order');

                $save['id'] = $order['id'];
                $save['status'] = 2;
                $o->save($save);
                file_put_contents('log.txt',"【sql】:\n".M()->getLastSql()."\n");

                if($order['is_recharge'] == 2){ //充值订单处理
                    $recharge = M('recharge');
                    $save1['id'] = $order['recharge_id'];
                    $save1['status'] = 2;
                    $recharge->save($save1);


                    $this->rechargeWard($order['user_id'],$order['amount']);
                    $this->zhitui($order['id'],$order['amount']);


                }else if($order['is_recharge'] == 1){//普通订单处理
                    $save2['id'] = $order['user_id'];
                    $u = M('user')->find($order['user_id']);
                    $save2['z2'] = $u['z2'] + $order['amount'];

                    M('user')->save($save2);

                    $this->checklevel($order['user_id']);
                    $this->zhitui($order['id'],$order['amount']);


                    $this->add_log($u['id'],$order['amount'],2,'品牌补贴','+');
                    $this->add_fmoney($u['id'],$u['id'],$order['amount'],2,4,'品牌补贴');
                }else{

                }



            }
        }
    }


    protected function add_log($user_id,$amount=0,$type,$tips,$flag='+')
    {
        $map['user_id'] = $user_id;
        $map['amount'] = $amount;
        $map['type'] = $type;
        $map['create_time'] = time();

        // $map['account'] = $account;
        $map['flag'] = $flag;

        $map['tips'] = $tips;
        return M('money_detail')->add($map);
    }


    /**
     * 添加奖金纪录明细
     * @param $user_id
     * @param $from_id
     * @param $amount
     * @param $type
     * @param $reward_type
     * @param $tips
     * @return mixed
     */
    protected function  add_fmoney($user_id,$from_id,$amount,$type,$reward_type,$tips)
    {
        $map['user_id'] =$user_id;
        $map['from_id'] = $from_id;
        $map['amount'] = $amount;
        $map['type'] = $type;
        $map['reward_type'] = $reward_type;
        $map['tips'] = $tips;
        $map['create_time'] = time();
        return M('fmoney')->add($map);
    }

    /**
     * 会员注册
     */
    public function register()
    {
        if(IS_POST){
            $param = I('post.');
            if (empty($param['username'])){
                ajax_return(1,'Please enter nickname');
            }

//            if(empty($param['wallet_name'])){
//                ajax_return('1','钱包名称还未填写');
//            }
//            if(empty($param['phone'])){
//                ajax_return('1','手机号不能为空');
//            }
            if(empty($param['email'])){
                ajax_return('1','Mailboxes cannot be empty');
            }
//            if(!preg_phone($param['phone'])){
//                ajax_return(1,'手机号格式不正确');
//            }

            if (empty($param['password'])){
                ajax_return(1,'Enter your password');
            }

            if($param['password']!==$param['password_re']){
                ajax_return('1','The two passwords don`t match');
            }

            if (empty($param['password2'])){
                ajax_return(1,'Please enter your payment code');
            }
//            $user_login=M('user_login')->where(['ip'=>'117.81.247.28'])->find();
//            if($user_login){
//                ajax_return(1,'限制登录');
//            }
            $param['password'] = md5($param['password']);//登录密码
            $param['password2'] = md5($param['password2']);//支付密码
//			$param['payaddress']=$this->randomkeys(32);
			$param['invite']=randomkeys(5);
            $param['uid']=randNumbers(8);
			$param['invite_time']=time();


            //验证短信
            $code['mobile'] = $param['email'];
            $code['code'] = $param['code1'];

            if(!M('msg_list')->where($code)->find())
            {
                ajax_return(1,'The verification code is incorrect');
            }

            // if (!preg_match('/^[_0-9a-z]{3,16}$/i',$param['username'])){
            // }

            //手机号已经注册账号个数
//            $registerNum = M('user')->where(['phone'=>$param['phone']])->count();
//            if($registerNum >= 1){
//                ajax_return(1,'每个手机号只能注册一个账号，请更换手机号注册');
//            }
				
            //用户名是否存在
            if($this->user->where(array('username'=>$param['username']))->find()){
                ajax_return(1,'The user name is already registered');
            }

            //推荐人
//            $info = $this->user->getUserByname($param['higher']);
            $config=M('reward_config')->find();
            $info = $this->user->getUserByInvite($param['higher'],$config['hour']);
            if(!$info){
                ajax_return(1,'The invitation code does not exist or has expired');
            }else{
                if($info['status'] != 1){
                    ajax_return(1,'The recommender is not active');
                }
                $param['higher_id'] = $info['id'];//推荐人id
                M('user')->where(['id'=>$info['id']])->save(['pass_time'=>time()]);
                if($info['is_tj']==2){
                    M('user')->where(['id'=>$info['id']])->setInc('tj_time');
                }
            }


            //判断推荐人是否有100usdt或等值exuu
//            $zc=M('user')->where(array('username'=>$param['higher']))->find();
//            $usdtapi=$this->usdtapi();
//            $config=M('reward_config')->where('id=1')->find();


//            if($zc['z8']+$zc['frozen_z8']<100 && $zc['z1']+$zc['frozen_z1']<round(100*$usdtapi['rate']/$config['ptr17'],2)){
//            if($zc['z8']+$zc['frozen_z8']<100 && $zc['z1']+$zc['frozen_z1']<round(100/$config['ptr17'],2)){
//                ajax_return(1,'该推荐人资产不足100USDT或等值EXUU');
//            }

            #注册时间
            $param['create_time'] = time();
            $param['status'] = 1;   //默认激活
          	$time = time();
          	$number = rand(111111,999999);
         	$string = $time.$number;
          	$param['payaddress'] = md5($string);

//            $library=M('word')->select();
//            $library=M('bip39')->select();
//            $library=array_column($library,'word');
//            $arr=array();
//            for($i=0;$i<12;$i++){
//                do{
//                    $x = $library[array_rand($library)];
//                }while(in_array($x,$arr));
//                array_push($arr,$x);
//            }
//            $str=implode(' ',$arr);
//            $param['memory_word']=$str;//助记词（自己生成）

            if(!$id = $this->user->add($param)){ //$id插入数据返回的用户id
                ajax_return(1,'fail to register ');
            }

            //推荐关系
            $higher_member_id = $param['higher_id'];
            $db = M();
            $aa = $db->execute("call net_add_tj($id,$higher_member_id)");

            //接点关系
            $bb = $db->execute("call net_add_jd_auto($id,$higher_member_id)");



//            ajax_return(0,'注册成功',U('create_wallet',array('id'=>$id)));
            ajax_return(0,'registered successfully',U('Public/login'));
//            ajax_return(0,'注册成功',"https://www.pgyer.com/TatI");
        }else{

            if($username=I('get.higher')){
//                $username=M('user')->where(array('id'=>$username))->getField('username');
                $this->assign('higher',$username);
            }

            //区号
            $mobile_prefix = M('mobile_prefix')->select();
            $this->assign('mobile_prefix',$mobile_prefix);
            // $this->assign('rand_user','AM'.randNumber());
            $this->display();
        }
    }

    //登录记录
    private function makeAdminLogin()
    {
        $map['create_time'] = time();
        $map['ip'] = get_client_ip();
        $map['username'] = session('User_yctr.username');

        return M('user_login')->add($map);
    }

    public function area_choose(){
        if(IS_POST){
            $province_id = I('post.pro_id');
            $region = M('city')->where('father = "'.$province_id.'"')->select();
            $opt = '<option>选择市</option>';
            foreach($region as $key=>$val){
                $opt .= "<option value='{$val['cityid']}'>{$val['city']}</option>";
            }
            echo json_encode($opt);
        }else{
        }
    }

    public function city_choose(){
        if(IS_POST){
            $city_id = I('post.city_id');
            $region = M('area')->where('father = "'.$city_id.'"')->select();
            $optt = '<option>选择区</option>';
            foreach($region as $key=>$val){
                $optt .= "<option value='{$val['areaid']}'>{$val['area']}</option>";
            }
            echo json_encode($optt);
        }else{
        }
    }

    public function province_choose(){
        if(IS_POST){
            $pianqu_id = I('post.pianqu_id');
            $region = M('province')->where('father = "'.$pianqu_id.'"')->select();

            $optt = '<option>--请选择省区--</option>';
            foreach($region as $key=>$val){
                $optt .= "<option value='{$val['provinceid']}'>{$val['province']}</option>";
            }
            echo json_encode($optt);
        }else{
        }
    }


    //usdt汇率
    public function usdtapi(){
        $url = "https://api.jinse.com/v3/coin/detail?slugs=tether&_source=m";
        $btcData = $this->https_request($url);
        $btcData = json_decode($btcData, true);
        return $btcData;
    }

    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }



    public function explode_address($p,$c,$a){

        $provinceid = M('province')->where('provinceid = "'.$p.'"')->find();
        $city = M('city')->where('cityid = "'.$c.'"')->find();
        $area = M('area')->where('areaid = "'.$a.'"')->find();
        return $provinceid['province'].$city['city'].$area['area'];

    }

    //三级分销奖


    public function zfb_return()
    {
        Vendor('Alipay.pagepay.service.AlipayTradeService');
        $arr=$_GET;

        $config = C("alipay");
        $alipaySevice = new \AlipayTradeService($config);
        $result = $alipaySevice->check($arr);

        echo "<script>window.location.href='/index.php/Index/index.html';</script>";
        /* 实际验证过程建议商户添加以下校验。
        1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
        2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
        3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
        4、验证app_id是否为该商户本身。
        */
        if(json_encode($arr)) {//验证成功
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //请在这里加上商户的业务逻辑程序代码

            //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
            //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表

            //商户订单号

            // $out_trade_no = htmlspecialchars($_GET['out_trade_no']);

            // //支付宝交易号

            // $trade_no = htmlspecialchars($_GET['trade_no']);
            echo "<script>window.location.href='/index.php/Index/index.html';</script>";
            // echo "验证成功<br />外部订单号：".$out_trade_no;

            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "验证失败";
        }
    }


    public function zfb_notify()
    {

        Vendor('Alipay.pagepay.service.AlipayTradeService');
        $config = C("alipay");
        $arr=$_POST;

        $alipaySevice = new \AlipayTradeService($config);

        $alipaySevice->writeLog(var_export($_POST,true));
        $result = $alipaySevice->check($arr);

        /* 实际验证过程建议商户添加以下校验。
        1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
        2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
        3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
        4、验证app_id是否为该商户本身。
        */

        if(json_encode($arr)) {//验证成功
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //请在这里加上商户的业务逻辑程序代


            //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——

            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表

            //商户订单号

            $out_trade_no = $_POST['out_trade_no'];

            //支付宝交易号

            $trade_no = $_POST['trade_no'];

            //交易状态
            $trade_status = $_POST['trade_status'];


            //   if($_POST['trade_status'] == 'TRADE_FINISHED') {

            // //判断该笔订单是否在商户网站中已经做过处理
            // 	//如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
            // 	//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
            // 	//如果有做过处理，不执行商户的业务程序

            // //注意：
            // //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
            //   }
            if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                $where['order_id'] = $out_trade_no;
                $order = M('order')->where($where)->find();
                $userCenterLevel = M('user')->where(['id'=>$order['user_id']])->getField('center_level');
                if($order['status']>1){
                    echo "success";
                    exit();
                }
                $save1['id'] = $order['id'];
                $save1['status'] = 2;
                $save1['time_end'] = time();
                M('order')->save($save1);

                //执行存储过程
                $db = M();
                $aa = $db->execute("call zhixing_ri()");

                //会员购买发报单奖给各个等级中心
                //当前用户中心等级
                if($userCenterLevel == 0)
                {
                    $userCenterLevel = 1;
                }
                $wh['user_id'] = $order['user_id'];
                $wh['is_default'] = 2;
                //获取用户默认地址
                $address = M('address')->where($wh)->field('prince,city,area')->find();

                $whereAd['b.center_level'] = array('gt',$userCenterLevel);
                $whereAd['a.prince'] = $address['prince'];
                $whereAd['a.city'] = $address['city'];
                $whereAd['a.area'] = $address['area'];
                $whereAd['a.is_default'] = 2; //选择默认地址
                $list = M('address')->alias('a') //中心所有用户
                ->join('__USER__ b ON a.user_id=b.id')
                    ->where($whereAd)
                    ->field('b.id,b.username,b.center_level,a.longitude,a.latitude,a.street')
                    ->select();
                if($list)
                {
                    for($a=0;$a<count($list);$a++)
                    {
                        if($list[$a]['center_level'] == 2)
                        {
                            $cf = M('reward_config')->where(['id'=>1])->getField('ptr59');
                        }
                        if($list[$a]['center_level'] == 3)
                        {
                            $cf = M('reward_config')->where(['id'=>1])->getField('ptr60');
                        }
                        if($list[$a]['center_level'] == 4)
                        {
                            $cf = M('reward_config')->where(['id'=>1])->getField('ptr61');
                        }
                        $getMoney = $order['amount']*$cf/100;
                        //进入重销比例
                        $rate = M('reward_config')->where(['id'=>1])->getField('ptr39');
                        $srate = M('reward_config')->where(['id'=>1])->getField('ptr45');
                        if($getMoney > 0)
                        {
                            M('user')->where(['id'=>$list[$a]['id']])->setInc('z3',$getMoney*(100-$rate)/100*(100-$srate)/100);
                            M('user')->where(['id'=>$list[$a]['id']])->setInc('z2',$getMoney*$rate/100*(100-$srate)/100);

                            //添加记录,报单奖-购物积分
                            $fmoney['user_id'] = $list[$a]['id'];
                            $fmoney['from_id'] = $order['user_id'];
                            $fmoney['amount'] = $getMoney*(100-$rate)/100*(100-$srate)/100;
                            $fmoney['type'] = 0; //物流积分
                            $fmoney['reward_type'] = 1;
                            $fmoney['create_time'] = time();
                            $fmoney['tips'] = '区域奖-购物积分';
                            M('fmoney')->add($fmoney);

                            //添加记录,报单奖-重销积分
                            $fmoney['amount'] = $getMoney*$rate/100*(100-$srate)/100;
                            $fmoney['create_time'] = time();
                            $fmoney['tips'] = '区域奖-重销积分';
                            M('fmoney')->add($fmoney);
                        }
                    }
                }
            }
            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

            echo "success";		//请不要修改或删除

        }else {
            //验证失败
            echo "fail";	//请不要修改或删除

        }
    }

    public function ttt()
    {

    }

    public function add_min_sale($orderinfo)
    {
        //商品销量,库存
        if($orderinfo['goods_id']){
            $goodsinfo = M('goods')->find($orderinfo['goods_id']);

            $save['id']= $goodsinfo['id'];
            $save['sale'] = $goodsinfo['sale']+ $orderinfo['num'];
            $save['repertory'] = $goodsinfo['repertory'] - $goodsinfo['num'];
            M('goods')->save($save);

        }else{//有子订单
            $where['order_id'] = $orderinfo['id'];
            $suborderinfo = M('suborder')->where($where)->select();
            foreach ($suborderinfo as $k => $v)
            {
                $goodsinfo = M('goods')->find($v['goods_id']);

                $save['id']= $goodsinfo['id'];
                $save['sale'] = $goodsinfo['sale']+ $v['num'];
                $save['repertory'] = $goodsinfo['repertory'] - $v['num'];
                M('goods')->save($save);

            }
        }






    }




}